/**
 * 
 */
package util;

import wdmsim.Link;

/**
 * @author alissonslp
 *
 */
public class WeightedGraph {

	/** */
	private int numNodes;
	/** */
    private Link[][] edges;  // adjacency matrix
    
    /**
     * 
     * @param n
     */
    public WeightedGraph(int n) {
        edges = new Link[n][n];
        numNodes = n;
        
        for(int i = 0; i < n; i++)
        	for(int j = 0; j < n; j++)
        		edges[i][j] = null;
    }
    
    /**
     * 
     * @param g
     */
    public WeightedGraph(WeightedGraph g) {
        numNodes = g.numNodes;
        edges = new Link[numNodes][numNodes];
        for (int i = 0; i < numNodes; i++) {
            for (int j = 0; j < numNodes; j++) {
                edges[i][j] = g.getEdge(i, j);
            }
        }
    }
    
    /**
     * 
     * @return
     */
    public int size() {
        return numNodes;
    }
    
    /**
     * 
     * @param i
     * @param j
     * @return
     */
    public Link getEdge(int i, int j) {
    	return edges[i][j];
    }
    
    /**
     * 
     * @param source
     * @param target
     * @param link
     * @throws Exception
     */
    public void addEdge(int source, int target, Link link) throws Exception {
    	if(source<size() && target<size()) {
    		edges[source][target] = link;
    	} else {
    		throw new IllegalArgumentException("addEdge->WeightedGraph");
    	}
    }

    /**
     * 
     * @param source
     * @param target
     * @return
     */
    public boolean isEdge(int source, int target) {
        return edges[source][target] instanceof Link;
    }

    /**
     * 
     * @param source
     * @param target
     */
    public void removeEdge(int source, int target) {
        edges[source][target] = null;
    }
    
    /**
     * 
     * @param source
     * @param target
     * @return
     */
    public int getWeight(int source, int target) {
        return edges[source][target].getTotalAvailableBandwidth();
    }
    
    public int getNumFreeWavelengths(int source, int target) {
    	return edges[source][target].getNumFreeWavelengths();
    }
    /**
     * 
     * @param source
     * @param target
     * @param l
     */
    public void setWeight(int source, int target, Link l) {
        edges[source][target] = l;
    }
    
    /**
     * 
     * @param node
     */
    public void removeNode(int node) {
    	int i;
    	
    	for(i = 0; i < numNodes; i++) {
    		removeEdge(node, i);
    		removeEdge(i, node);
    	}
    }

    /**
     * 
     * @param vertex
     * @return
     */
    public int[] neighbors(int vertex) {
        int count = 0;
        for (int i = 0; i < edges[vertex].length; i++) {
            if (edges[vertex][i] instanceof Link) {
                count++;
            }
        }
        final int[] answer = new int[count];
        count = 0;
        for (int i = 0; i < edges[vertex].length; i++) {
            if (edges[vertex][i] instanceof Link) {
                answer[count++] = i;
            }
        }
        return answer;
    }

    @Override
    public String toString() {
        String s = "";
        for (int j = 0; j < edges.length; j++) {
            s += Integer.toString(j) + ": ";
            for (int i = 0; i < edges[j].length; i++) {
                if (edges[j][i] instanceof Link) {
                    s += Integer.toString(i) + ":" + Double.toString(edges[j][i].getWeight()) + " ";
                }
            }
            s += "\n";
        }
        return s;
    }

	/**
	 * @param id
	 * @param dst
	 */
	public void getMultidomainCost(int id, int dst) {
		// TODO Auto-generated method stub
		
	}
    
}
